package com.datastructrue;

public class MyDoubleLinkedMain {
    public static void main(String[] args) {
        Node head = new Node(12, null);
        MySingleLinkedList mySingleLinkedList = new MySingleLinkedList(head);
        mySingleLinkedList.pushHead(10);
        mySingleLinkedList.pushTail(99);
        mySingleLinkedList.pushTail(1);
        mySingleLinkedList.pushHead(2);
        mySingleLinkedList.forEach();
        mySingleLinkedList.deleteHead();
        mySingleLinkedList.deleteTail();
        mySingleLinkedList.delete(12);
        mySingleLinkedList.forEach();
    }
}

/**
 * 双端链表实现
 */
class MyDoubleLinked {
    //头节点
    private Node head;
    // 尾节点
    private Node tail;

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public Node getTail() {
        return tail;
    }

    public void setTail(Node tail) {
        this.tail = tail;
    }

    /**
     * 头插
     */
    public void pushHead(int data) {
        Node node = new Node(data, head);
        head = node;
    }

    /**
     * 尾插
     *
     * @param data
     */
    public void pushTail(int data) {
        if (head == null) {
            pushHead(data);
        } else {
            Node node = new Node(data, null);
            tail.setNext(node);
            tail = node;
        }
    }

    /**
     * 查找指定节点
     */
    public Node find(int data) {
        if (head == null) {
            return null;
        }
        Node node;
        Node currentNode = head;
        while (true) {
            if (currentNode == null || currentNode.getData() == data) {
                node = currentNode;
                break;
            }
            currentNode = currentNode.getNext();
        }
        return node;
    }

    /**
     * 删除头节点
     */
    public void deleteHead() {
        if (head != null) {
            head = head.getNext();
        }
    }

    /**
     * 删除尾节点
     */
    public void deleteTail() {
        if (head != null) {
            Node currentNode = head;
            while (true) {
                if (currentNode.getNext() == null) {
                    break;
                }
                if (currentNode.getNext().getNext() == null) {
                    currentNode.setNext(null);
                    tail = currentNode;
                    break;
                }
                currentNode = currentNode.getNext();
            }
        }
    }

    /**
     * 删除指定元素
     */
    public void delete(int data) {
        if (head != null) {
            if (head.getData() == data) {
                deleteHead();
                return;
            }
            //当前节点
            Node currentNode = head;
            //当前节点父级节点信息
            Node dadNode = head;
            while ((currentNode = currentNode.getNext()) != null) {
                if (currentNode.getData() == data) {
                    dadNode.setNext(currentNode.getNext());
                }
                dadNode = currentNode;
            }
        }
    }

    /**
     * 遍历
     */
    public void forEach() {
        if (head != null) {
            Node currentNode = head;
            do {
                System.out.print(currentNode.getData() + " --> ");
            } while ((currentNode = currentNode.getNext()) != null);
            System.out.print("null");
            System.out.println();
        }
    }

}
